import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    flag:false,  // 判断是否引入mock
    type: '',  //signIn,signUp  区分获取接口时的url
    user: {
        username: '',
        age: '',
        gender: ''
    },  //存储用户信息
    recList: [],  //推荐页的新闻列表
    hotList: [],  //热门页的新闻列表
  },
  mutations: {
      // 清空列表 在刷新时调用 重新给列表赋值
      clearList(state, payload){
          if(payload == "recList"){
              state.recList = []
          }else if(payload == "hotList"){
              state.hotList = []
          }
      },

      clearUser(state, payload){
        state.type = '',
        state.user = {
            username: '',
            age: '',
            gender: ''}
        state.recList = []
        state.hotList = []
      },

      //点进新闻详情页时触发，让阅读次数增加
      numChange(state, payload) {
          let reg = /NewsInfo\//
          if(payload.item == 'recList'){
              for (let i = 0; i < state.recList.length; i++) {
                  if (state.recList[i].news_id == payload.path.split(reg)[1]) {
                      state.recList[i].read_num++
                  }
              }
          }else if(payload.item == 'hotList'){
              for (let i = 0; i < state.hotList.length; i++) {
                  if (state.hotList[i].news_id == payload.path.split(reg)[1]) {
                      state.hotList[i].read_num++
                  }
              }
          }
      },

      //点击喜欢或者收藏时触发，让相应次数增加或者减少
      actionChange(state, payload){
          if(payload.type == 'likes'){
              for(let i = 0; i<state.recList.length; i++){
                  if(state.recList[i].news_id == payload.id){
                      state.recList[i].likes +=  payload.num
                  }
              }
              for(let i = 0; i<state.hotList.length; i++){
                  if(state.hotList[i].news_id == payload.id){
                      state.hotList[i].likes += payload.num
                  }
              }
          }else if(payload.type == 'collections'){
              for(let i = 0; i<state.recList.length; i++){
                  if(state.recList[i].news_id == payload.id){
                      state.recList[i].collections += payload.num
                  }
              }
              for(let i = 0; i<state.hotList.length; i++){
                  if(state.hotList[i].news_id == payload.id){
                      state.hotList[i].collections += payload.num
                  }
              }
          }
          
      }
  },
});
